package com.koron.standard.deviceClasssify.bean.excel;

import cn.hutool.core.util.NumberUtil;
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ContentFontStyle;
import com.alibaba.excel.annotation.write.style.ContentStyle;
import com.alibaba.excel.enums.BooleanEnum;
import com.alibaba.excel.enums.poi.BorderStyleEnum;
import com.alibaba.excel.enums.poi.HorizontalAlignmentEnum;
import com.alibaba.excel.enums.poi.VerticalAlignmentEnum;
import com.koron.common.core.util.CodeTools;
import com.koron.standard.deviceClasssify.bean.po.Type;
import com.koron.standard.deviceClasssify.bean.po.TypeParams;
import com.koron.standard.deviceClasssify.excel.TypeStringStringConvertor;
import lombok.Data;

import java.util.Arrays;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * 设备分类导入实体
 * @author jzp
 * @since 2022/5/11
 */
@Data
@ExcelIgnoreUnannotated
public class TypeExcel {

    @ContentStyle(verticalAlignment = VerticalAlignmentEnum.CENTER, dataFormat = 49)
    @ExcelProperty(index = 0)
    // @ExcelProperty(value = "层级编号")
    private String order;

    @ContentStyle(verticalAlignment = VerticalAlignmentEnum.CENTER, dataFormat = 49)
    @ContentFontStyle(bold = BooleanEnum.TRUE)
    @ExcelProperty(index = 1)
    // @ExcelProperty(value = "分类编码")
    private String code;

    @ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER)
    @ContentFontStyle(bold = BooleanEnum.TRUE)
    @ExcelProperty(index = 2)
    // @ExcelProperty(value = "分类名称")
    private String name;

    @ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER)
    @ContentFontStyle(bold = BooleanEnum.TRUE)
    @ExcelProperty(index = 3, converter = TypeStringStringConvertor.class)
    // @ExcelProperty(value = "分类类型")
    private String isVirtual;

    @ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER)
    @ExcelProperty(index = 4, converter = TypeStringStringConvertor.class)
    // @ExcelProperty(value = "专业")
    private String major;

    @ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER, borderRight = BorderStyleEnum.THIN)
    @ExcelProperty(index = 5, converter = TypeStringStringConvertor.class)
    // @ExcelProperty(value = "基本参数组")
    private String groupId;

    @ExcelProperty(index = 6)
    // @ExcelProperty(value = "参数名称")
    private String paramName;

    @ExcelProperty(index = 7, converter = TypeStringStringConvertor.class)
    // @ExcelProperty(value = "数据类型")
    private String dataType;

    @ExcelProperty(index = 8)
    // @ExcelProperty(value = "数据长度")
    private String dataLength;

    @ExcelProperty(index = 9)
    // @ExcelProperty(value = "精度")
    private String dataPrecision;

    @ExcelProperty(index = 10, converter = TypeStringStringConvertor.class)
    // @ExcelProperty(value = "输入类型")
    private String enterType;

    @ExcelProperty(index = 11, converter = TypeStringStringConvertor.class)
    // @ExcelProperty(value = "是否必填")
    private String isMust;

    @ExcelProperty(index = 12)
    // @ExcelProperty(value = "数据计量单位")
    private String dataUnit;

    @ExcelProperty(index = 13)
    // @ExcelProperty(value = "参数说明")
    private String desc;

    @ContentStyle(borderRight = BorderStyleEnum.THIN)
    @ExcelProperty(index = 14)
    // @ExcelProperty(value = "参数列表")
    private String dataText;

    public String getCode() {
        // 层级编号和分类编码二选一，两者都有时以分类编码为准
        if (code == null && order != null) {
            code = Arrays.stream(order.split("\\."))
                    .map(e -> String.format("%03d", Integer.valueOf(e)))
                    .collect(Collectors.joining());
        }
        return code;
    }

    public Type buildType() {
        Type type = new Type();
        // id
        type.setId(CodeTools.getCode32());

        // 基本信息
        type.setCode(getCode());
        type.setName(getName());
        type.setMajor(getMajor());
        type.setIsVirtual(NumberUtil.parseInt(getIsVirtual()));
        type.setGroupId(getGroupId());

        // 当前节点信息
        type.setLevel(code.length() / 3);
        type.setLevelCode(Integer.valueOf(code.substring(code.length() - 3)));
        type.setSort(type.getLevelCode());
        String levelPath = IntStream.range(0, code.length() / 3)
                .map(e -> e * 3)
                .mapToObj(e -> code.substring(e, e + 3))
                .map(Integer::valueOf)
                .map(Object::toString)
                .collect(Collectors.joining("/"));
        type.setLevelPath(levelPath);
        return type;
    }

    public TypeParams buildParam(String preTypeId, Integer sort) {
        TypeParams typeParams = new TypeParams();
        // id
        typeParams.setId(CodeTools.getCode32());
        typeParams.setGroup("2"); // 都是技术参数
        typeParams.setTypeId(preTypeId);

        // 基本信息
        typeParams.setParamName(getParamName());
        typeParams.setIsMust(NumberUtil.parseInt(getIsMust()));
        typeParams.setEnterType(getEnterType());
        typeParams.setDataType(getDataType());
        typeParams.setDataLength(NumberUtil.parseInt(getDataLength()));
        typeParams.setDataPrecision(NumberUtil.parseInt(getDataPrecision()));
        typeParams.setDataUnit(getDataUnit());
        typeParams.setDataText(getDataText());
        typeParams.setDesc(getDesc());
        typeParams.setSort(sort);
        return typeParams;
    }
}
